package com.suoluo.zenadmin.controllers.admin;

import javax.servlet.http.HttpSession;

import net.paoding.rose.web.Invocation;

public final class AdminLoginManager {

	/**
	 * 系统用户的编号
	 */
	private static final int ADMIN_USER_ID = -1000;
	
	private static final String ADMIN_LOGIN_KEY = "ADMIN_LOGIN_KEY";
	
	private static final String IS_DELETE = "IS_DELETE";
	
	private AdminLoginManager(){
		
	}
	
	public static boolean isLogined(Invocation inv){
		Object loginInfo = inv.getRequest().getSession().getAttribute(ADMIN_LOGIN_KEY);
		return loginInfo != null;
	}
	
	public static void login(Invocation inv,Object loginInfo){
		HttpSession session = inv.getRequest().getSession();
		LoginInfo loginWrapper = new LoginInfo(ADMIN_USER_ID, loginInfo);
		session.setAttribute(ADMIN_LOGIN_KEY, loginWrapper);
	}
	
	public static void isDelete(Invocation inv,int isDelete) {
		HttpSession session = inv.getRequest().getSession();
		session.setAttribute(IS_DELETE, isDelete);
	}
	
	public static boolean getIsDelete(Invocation inv) {
		Object isDel = inv.getRequest().getSession().getAttribute(IS_DELETE);
		if(isDel != null) {
			int isDelete = (Integer)isDel;
			if(isDelete == 1) {
				return true;
			}
		}
		
		return false;
	}
	public static int getLoginUserID(Invocation inv){
		if (isLogined(inv)){
			return ADMIN_USER_ID;
		}else{
			return 0;
		}
	}
	
	
	private static class LoginInfo{
		private int userID;
		private Object loginInfo;
		
		public LoginInfo(int userID, Object loginInfo) {
			super();
			this.userID = userID;
			this.loginInfo = loginInfo;
		}
		public int getUserID() {
			return userID;
		}
		public void setUserID(int userID) {
			this.userID = userID;
		}
		public Object getLoginInfo() {
			return loginInfo;
		}
		public void setLoginInfo(Object loginInfo) {
			this.loginInfo = loginInfo;
		}
		
		
	}
}
